/*
 * This file contains all the javascript functions that use by
 * Syn asset management plugin in Synergy digital asset management
 * system.
 * Create by: Weranga Kaluarachchi
 * Date: 18/04/2010
 */


// supporting file types
var fileTypes = ["doc", "docx", "txt", "pdf", "ppt", "pptx", "xls", "xlsx", "rtf", "mp3", "wav", "wma", "mpg", "mpeg", "3gp", "jpg", "jpeg", "png", "gif"];


/*
 * Validate the maximum length of a component
 * @param	componenet	component that wants to validate
 * @param	int	maximum length 
 */
function textDescriptionLimit(field, maxlen) {
	if (field.value.length > maxlen) {
		alert('Description has exceeded the maximum length!');
		return;
	}	
} 



/*
 * Load asset to replace
 * @param	int	asset ID
 * @return	true or false
 */
function replaceAsset(resID) {
	if(document.getElementsByName('btnReplace') == null) {
        return;
    }
	var lb = document.getElementById('ldbSynAssetKeywords');
	for(var i = 0; i < lb.length; i++){
    	lb.options[i].selected = false;
    }
	document.getElementById('btnUpload').innerHTML = "Upload";
	enableUpload();
	document.getElementById('btnSynAssetCancel').innerHTML = "Cancel Replace";
	document.getElementById('btnSynAssetCancel').disabled = false;
	document.getElementById('txtTitle').value = "";
    document.getElementById('txtAssetDescription').value = "";
    document.getElementById('lblSynAssetMode').value = "Replace asset mode";
    setLabel('lblSynAssetMode', 'Replace asset mode');    
	document.getElementById('hdResID').value = resID;
	document.getElementById('updateMode').value = "replaceAsset";
	document.getElementById('flUserfile').disabled = false;
    document.getElementById('addresource').style.display="inline"; 
    document.getElementById('btnToggleDisplay').innerHTML = "[Hide]<br /><br />";
  
}



function clearReplaceMode() {
	if(document.getElementsByName('btnSynAssetCancel') == null) {
        return;
    }
	
	document.getElementById('btnSynAssetCancel').innerHTML = "Clear";
	document.getElementById('hdResID').value = "";
	setLabel('lblSynAssetMode', 'Upload new asset mode'); 
	document.getElementById('btnUpload').disabled = true;
	 document.getElementById('flUserfile').disabled = false;
	   
}

/*
 * Populate asset to edit information
 * @param	int	asset ID
 * @return	true or false
 */
function editAssetInfo(resID) {
	if(document.getElementsByName('btnEditInfo') == null) {
        return;
    }
    var lb = document.getElementById('ldbSynAssetKeywords');
	var resTitle = document.getElementById('tmpResTitle'+resID).value;
    var resDescription = document.getElementById('tmpDescription'+resID).value;
    var keyWords = document.getElementById('tmpKeywordID'+resID).value;
    var keyWord = keyWords.split("#");
   
    for(var i = 0; i < lb.length; i++){
    	lb.options[i].selected = false;
    }
    for(var i = 0; i < lb.length; i++){
	    for(var x in keyWord) {
	    	if(lb.options[i].value==keyWord[x]) {
	   	 		lb.options[i].selected = true;
	   	 		break;
	   	 	}
	    }
    }
    document.getElementById('btnUpload').innerHTML = "Update";
    enableUpload();
    document.getElementById('btnSynAssetCancel').innerHTML = "Cancel Update";
    document.getElementById('btnSynAssetCancel').disabled = false;
    document.getElementById('flUserfile').disabled = true;    
    
    document.getElementById('txtTitle').value = resTitle;
    document.getElementById('hdResID').value = resID;
    document.getElementById('txtAssetDescription').value = resDescription;  
    setLabel('lblSynAssetMode', 'Edit asset information mode');
    //document.getElementById('lblSynAssetMode').value = "Edit asset information mode";
    document.getElementById('updateMode').value = "editInfo";
    document.getElementById('addresource').style.display="inline"; 
    document.getElementById('btnToggleDisplay').innerHTML = "[Hide]<br /><br />";
  
}

/*
 * Validate the all the inputs of the asset
 * @return	true or false
 */
function validateUpload() {
	var isUpCheck = true;
	if(document.getElementById('flUserfile') == null) {
        return;
    }
	else if(document.getElementById('txtTitle') == null) {
        return;
    }    
	else if(trimAsset(document.getElementById('txtTitle').value) == "" ) {
		alert("Title can not be blank!");
		isUpCheck = false;
	}
	else if(document.getElementById('updateMode').value != "editInfo") {
		if(!validateFileType()) {
			isUpCheck = false;			
		}
	}
	
	//document.getElementById('btnUpload').innerHTML = "Upload";
	return isUpCheck;	
}

/*
 * Confirm delete an asset
 * @return true or false
 */
function confirmAssetDelete(resID) {
	if(document.getElementsByName('btnDelete') == null) {
        return;
    }
	var con = "";
	var isDelete = false;
	if(resID != 0) {
		con = document.getElementById('contributorName'+resID).value;
		con = con+"'s ";
	}    
    if(confirm("Do you really want to delete "+con+" asset?")) {
    	isDelete = true;    
    }
    return isDelete;
}


/*
 * Validate the file type that upload
 * @return true or false
 */
function validateFileType() {
	if(document.getElementById('flUserfile') == null) {
        return;
    }
    var isCheck = true;
    var bValue = document.getElementById('flUserfile').value;
          
    if(bValue.length==0) { 
    	alert("Please select the asset first!");       
        //setError('lblAssetBrowse', 'Please select the resource!');        
        isCheck = false;
    } 
    else if(document.getElementById('hdResID').value != "") {
    	if(document.getElementById('updateMode').value == "replaceAsset") {
    		if(confirm('Are you sure you want to replace this asset?')) {
        		return true;
        	} 
    		return false;
    	}    	   	
    }  
    else {
        var splitType = bValue.split(".");
        var i=0;
        for(i=0; i<fileTypes.length; i++) {
            if(splitType[splitType.length-1].toLowerCase() == fileTypes[i]) {
            	isCheck = true;
               	break;	                
            }
            else {            	
                isCheck = false;                               
            }
        }
        if(!isCheck) {
        	alert("Unsupported file type: "+splitType[splitType.length-1]);
        }        
    }   
    return isCheck;
}


/*
 * Clear error text in error label
 * @param   error label
 */
function clearLabel(errorlabel) {
	if(document.getElementById(errorlabel) == null) {
        return;
    }
    // for IE browser   
    if(navigator.appName == "Microsoft Internet Explorer") {
        document.getElementById(errorlabel).innerText = "";   
    }
    // for firefox browser 
    else if(navigator.appName == "Netscape") {
        document.getElementById(errorlabel).textContent = "";
    }     
}

/*
 * Set error text in error label
 * @param   error label
 * @prama   error message
 */
function setLabel(errorlabel, errorMsg) {
    if(document.getElementById(errorlabel) == null) {
        return;
    }
    if(navigator.appName == "Microsoft Internet Explorer") {
        // for IE browser
        document.getElementById(errorlabel).innerText = errorMsg;
    }
    else if(navigator.appName == "Netscape") {   
        // for firefox browser
        document.getElementById(errorlabel).textContent = errorMsg;
    }      
}


/*
 * This method is using to toggle visibility of any div element.
 * @param element ID of the element wants toggle
 * @param object that trigger the function
 */
 function toggleAddResource(objID, me) { 
	 if(document.getElementById(objID) == null) {
	        return;
	    }
	 if (document.getElementById(objID).style.display == "none") { 
	  	document.getElementById(objID).style.display="inline"; 
	  	me.innerHTML = "[Hide]<br /><br />";
	 } 
	 else {
		document.getElementById(objID).style.display="none"; 
	    me.innerHTML = "[Add a new asset]";
	 }
}

 

/*
 * Toggle enable/disable upload button
 */
function enableUpload() {
    if((document.getElementById('chkAgree') == null)) {
        return;
    }
    if(document.getElementById('chkAgree').checked) {
       document.getElementById('btnUpload').disabled = false;  
     }
    else {
    	document.getElementById('btnUpload').disabled = true;
    }    
}


/*
 * Trim a string at the begining and at the end
 * @param	string which wants to trim
 * @return	string after trim 
 */
function trimAsset(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


